package com.solr;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Test;

import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @program: Solr
 * @description:
 * @author: Dawson.Zhang
 * @create: 2020-03-14
 **/
public class Solrfuxi1 {
    public static String BASE_URL = "http://192.168.117.131:8080/solr/collection1";

    public static HttpSolrClient getClient() {
        return new HttpSolrClient.Builder(BASE_URL).build();
    }

    @Test
    public void addDoc() throws IOException, SolrServerException {
        HttpSolrClient client = getClient();

        SolrInputDocument solrInputFields = new SolrInputDocument();
        solrInputFields.addField("id", "515125");
        solrInputFields.addField("name", "阿发送到发送到发斯蒂芬发送到");
        client.add(solrInputFields);
        client.commit();
    }
    @Test
    public void searCh(){
        HttpSolrClient client = getClient();
        SolrQuery solrQuery = new SolrQuery();
        solrQuery.set("q","name:搞什么");
        try {
            QueryResponse query = client.query(solrQuery);

            SolrDocumentList results = query.getResults();
            System.out.println(results.getNumFound());
            for (SolrDocument result : results) {
                Object name = result.getFieldValue("name");
                System.out.println(name);
            }
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
//            "product_catalog_name": "幽默杂货",
//            "product_price": 18.9,
//            "product_name": "花儿朵朵彩色金属门后挂 8钩免钉门背挂钩2066",
//            "id": "1",
//            "product_picture": "2014032613103438.png",
//            "_version_": 1660792337241997300

    @Test
    public void searC1h () throws Exception{
        HttpSolrClient client = getClient();
        SolrQuery solrQuery = new SolrQuery();
        solrQuery.set("q","product_catalog_name:时尚卫浴 AND product_name:时尚");
        solrQuery.addHighlightField("product_catalog_name")
                  .addHighlightField("product_name");
        QueryResponse query = client.query(solrQuery);
        Map<String, Map<String, List<String>>> highlighting = query.getHighlighting();
        SolrDocumentList results = query.getResults();
        System.out.println(results.getNumFound()+"所有的条数");
        for (SolrDocument result : results) {
            Collection<String> fieldNames = result.getFieldNames();
            for (String fieldName : fieldNames) {
                System.out.println(fieldName);
                if ("id".equals(fieldName)){
                    Map<String, List<String>> stringListMap = highlighting.get(result.getFieldValue(fieldName));
                    List<String> product_catalog_name = stringListMap.get("product_catalog_name");
                    for (String s : product_catalog_name) {
                        System.out.println(s);
                    }
                    List<String> product_name = stringListMap.get("product_name");
                    for (String s : product_name) {
                        System.out.println(s);
                    }
                }
            }
        }
    }
}
